from chain import Chain
from atomsitecache import AtomsiteCache
from collections import OrderedDict
from exceptions import PDBentityNotFoundException

class Model :
	'''represents a model in a PDB file'''
	def __init__(s, dataconnector, mid) :
		s.dc, s.modelID = dataconnector, mid
		s.entry_id = s.dc.getCategory("entry","id","first")
	def getId(s) : return (s.entry_id, s.modelID)
	def getEntryId(s) : return s.entry_id
	def getModelId(s) : return s.modelID
	def makeChains(s) :
		s.chains = OrderedDict()
		modelchains = AtomsiteCache.getCache(s.dc).getHierarchy()
		for chid in modelchains[s.modelID] :
			s.chains[chid] = Chain(s.dc, s.modelID, chid)
	def getChains(s) :
		if not hasattr(s,'chains') : s.makeChains()
		return s.chains.values()
	def getChain(s,chainid) :
		if not hasattr(s,'chains') : s.makeChains()
		try : return s.chains[chainid]
		except KeyError : raise PDBentityNotFoundException("Chain with id %s not found." % str(chainid))
